package org.apache.ocean.main.search;

import java.util.ArrayList;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.ocean.main.search.cache.BooleanDocSetCacheKey;
import org.apache.ocean.main.search.cache.DistanceDocSetCacheKey;
import org.apache.ocean.main.search.cache.DocSetCacheKey;

public class Filters extends ArrayList<DocSetCacheKey> {
	public DocSetCacheKey getCacheKey() {
		if (size() == 1) {
			return get(0);
		} else if (size() > 1) {
			return new BooleanDocSetCacheKey(null, null, this);
		} else {
			return null;
		}
	}

	public static class Distance {
		private double latitude;
		private double longitude;
		private double distanceMiles;
		private int hashCode;

		public Distance(double latitude, double longitude, double distanceMiles) {
			this.latitude = latitude;
			this.longitude = longitude;
			this.distanceMiles = distanceMiles;
			HashCodeBuilder builder = new HashCodeBuilder(679, 121);
			builder.append(latitude);
			builder.append(longitude);
			builder.append(distanceMiles);
			hashCode = builder.toHashCode();
		}
		
		public boolean equals(Object object) {
			if (!(object instanceof Distance))
				return false;
			if (object == null)
				return false;
			Distance other = (Distance) object;
			EqualsBuilder equalsBuilder = new EqualsBuilder();
			equalsBuilder.append(latitude, other.latitude);
			equalsBuilder.append(longitude, other.longitude);
			equalsBuilder.append(distanceMiles, other.distanceMiles);
			return equalsBuilder.isEquals();
		}
		
		public int hashCode() {
			return hashCode;
		}

		public double getLatitude() {
			return latitude;
		}

		public double getLongitude() {
			return longitude;
		}

		public double getDistanceMiles() {
			return distanceMiles;
		}
	}
  
	public Distance getDistance() {
		for (DocSetCacheKey cacheKey : this) {
			if (cacheKey instanceof DistanceDocSetCacheKey) {
				DistanceDocSetCacheKey distanceDocSetCacheKey = (DistanceDocSetCacheKey)cacheKey;
				return distanceDocSetCacheKey.getDistance();
			}
		}
		return null;
	}
	
	public boolean hasDistance() {
		for (DocSetCacheKey cacheKey : this) {
			if (cacheKey instanceof DistanceDocSetCacheKey) {
				return true;
			}
		}
		return false;
	}
}
